package com.example.grpc.consume.web.resources;

import com.example.grpc.consume.model.dto.CompanyDto;
import com.example.grpc.consume.model.dto.RegisterDto;
import com.example.grpc.consume.model.dto.UserDto;
import com.example.grpc.consume.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file UserResource
 * @brief UserResource
 * @details UserResource
 * @date 2022-11-20
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-11-20               liyuncong          Created
 */
@RestController
@RequestMapping(
    path = "/api/v1/users",
    consumes = MediaType.APPLICATION_JSON_VALUE,
    produces = MediaType.APPLICATION_JSON_VALUE
)
public class UserResource {

    @Autowired
    private UserService userService;

    @PostMapping(path = "/search-by-company", params = "action=search")
    public ResponseEntity<List<UserDto>> searchUsersByCompany(
        @RequestBody CompanyDto companyDto
    ) {
        final List<UserDto> result = userService.findAllByCompany(companyDto);
        return ResponseEntity.ok(result);
    }

    @PostMapping(path = "/register", params = "action=register")
    public ResponseEntity<Boolean> register(
        @RequestBody RegisterDto registerDto
    ) {
        final Boolean register = userService.register(registerDto.getCompany(), registerDto.getUsers());
        return ResponseEntity.ok(register);
    }

    @PostMapping(path = "/all", params = "action=search")
    public ResponseEntity<List<UserDto>> searchAllUser() {
        final List<UserDto> allUsers = userService.findAllUsers();
        return ResponseEntity.ok(allUsers);
    }
}
